package com.xiaoma.lock.syn;


/**
 * synchronized代码块 锁对象
 */
public class ThreadTestffd {
    private Integer a = new Integer(1);
    private Integer b = new Integer(1);

//    private Integer a = 1;
//    private Integer b = 1;

//    private Person a=new Person("aa","1");
//    private Person b=a;


    public void A() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("A线程准备");
                //这个锁的对象和下面的线程中的锁，判断是否为同一个锁，是通过==来判断的，不是equals
                synchronized (a) {
                    System.out.println("A线程开始");
                    for (int i = 0; i < 10; i++) {
                        System.out.println("a" + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    public void B() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("B线程准备");
                synchronized (b) {
                    System.out.println("B线程开始");
                    for (int i = 0; i < 10; i++) {
                        System.out.println("b" + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }


    public static void main(String[] args) {
        ThreadTestffd test = new ThreadTestffd();
        test.A();
//        ThreadTestffd test2 = new ThreadTestffd();
        test.B();
    }
}